﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FuturamaKassaWithGUI;
using System.Threading;
using FuturamaKassa;
using FuturamaKassaWithGUI.Objects;

namespace FuturamaKassaWithGUI {
    public partial class Kassa : Form {

        private bool runScan;
        private static Kassa instance;
        private Thread scanThread;

        private decimal totalPrice;

        private List<Product> products;

        /**
         * Private constructor cause this class is a singleton.
         */
        private Kassa() {
            InitializeComponent();
            products = new List<Product>();
            runScan = false;
            totalPrice = 0.00m;
        }

        /**
         * Public static function to get the instance of this class or create a new depending if there already is one. (Singleton)
         */
        public static Kassa GetInstance() {
            lock (typeof(Kassa)) {
                if (instance == null) {
                    instance = new Kassa();
                }
                return instance;
            }
        }

        /**
         * Function to add a row to the products table.
         */
        public void addRow(List<Product> newProducts) {

            foreach (Product newP in newProducts) {

                bool check = true;

                foreach (Product p in products) {

                    if (p.Tag.Id == newP.Tag.Id) {
                        check = false;
                        break;
                    }
                }

                if (check) {
                    products.Add(newP);
                    this.dataGridView.Rows.Add(new string[] { newP.Name, "" + 1, newP.Price.ToString(), newP.Price.ToString() });

                    totalPrice += newP.Price;

                    this.totalText.Text = "€ " + totalPrice;
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {

        }
        
        /**
         * Function to close the application.
         */
        private void Exit(object Sender, EventArgs e) {
            
            if (scanThread == null) {
                scanThread.Abort();
            }
            Application.Exit();
        }

        /**
         * Function to start/stop the scanning.
         */
        private void Scan(object Sender, EventArgs e) {

            if (!runScan) {
                runScan = true;
                scanThread = new Thread(run);
                scanThread.IsBackground = true;
                scanThread.Start();
                this.scan.Text = "Stop Scanning";
            } else {
                runScan = false;
                scanThread.Abort();
                this.scan.Text = "Start Scanning";
            }
        }

        /**
         * Function that is run when the scanning starts. Will be run in another thread.
         */
        private void run() {
            new COMHandler();
        }

        /**
         * Function that simulates a pay off. In the PoC this just clears the table and reset the total.
         */
        private void payOff(object sender, EventArgs e) {

            if (products.Count > 0) {

                this.totalText.Text = "€ 0.00";
                this.totalPrice = 0.00m;

                this.products.Clear();
                this.dataGridView.Rows.Clear();

                this.dataGridView.Update();

                System.Windows.Forms.MessageBox.Show("You have paid.");
            } else {
                System.Windows.Forms.MessageBox.Show("There aren't any products scanned yet.");
            }
        }
    }
}